#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""

import os
import sys
import json
import requests

cur_dir = os.path.split(os.path.realpath(__file__))[0]
sys.path.append("%s/.." % cur_dir)
import config

sys.path.append("%s/../resource" % cur_dir)
from lijiacai.utils import tool


class ESSettings():
    url = tool.es_cluster[0]

    def set_max_result(self, index, max_=100000):
        url = self.url + f"/{index}/_settings"
        data = {
            "index": {"max_result_window": max_}
        }
        ret = requests.put(url, json=data).text
        print(ret)

    def set_mapping_vehicle(self):
        url = self.url + f"/{config.vehicle_base_info_index}"
        data = {
            "mappings": {
                "doc": {
                    "properties": {
                        "HEAD":
                            {"type": "keyword"},
                        "DATA":
                            {"type": "keyword"},

                    }}}}
        ret = requests.put(url, json=data).text
        print(ret)

    def set_mapping_search(self):
        url = self.url + f"/{config.es_index}"
        data = {
            "mappings": {
                "doc": {
                    "properties": {
                        "HEAD":
                            {"type": "keyword"},
                        "ACTION_TIME":
                            {"type": "date", "format": "yyyy-MM-dd HH:mm:ss"},
                        "NO":
                            {"type": "keyword"},
                        "NO_TYPE":
                            {"type": "keyword"},
                        "DATA":
                            {"type": "keyword"},
                        "DATA_SOURCE":
                            {"type": "keyword"},
                        "DATA_TYPE":
                            {"type": "keyword"}

                    }}}}
        ret = requests.put(url, json=data).text
        print(ret)

    def restore(self, index, dest):
        url = self.url + f"/_reindex"
        data = {"source": {"index": index}, "dest": {"index": dest}}
        ret = requests.post(url, json=data).text
        print(ret)


if __name__ == '__main__':
    es_settings = ESSettings()
    es_settings.set_mapping_vehicle()
    es_settings.set_mapping_search()
    es_settings.set_max_result(index=config.es_index, max_=100000)
    # es_settings.restore(index="main_v1",dest="main_v2")
    # es_settings.restore(index="vehicle_base_info_v1", dest="vehicle_base_info_v2")
